package com.example.shop_car_2208.order

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import androidx.lifecycle.MutableLiveData
import androidx.recyclerview.widget.LinearLayoutManager
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.blankj.utilcode.util.SPUtils
import com.blankj.utilcode.util.ToastUtils
import com.example.shop_car_2208.R
import com.example.shop_car_2208.adapter.OrderAdapter
import com.example.shop_car_2208.databinding.ActivityOrderBinding
import com.example.shop_car_2208.db.MyDBUtils
import com.example.shop_car_2208.db.MyDataBase
import com.example.shop_car_2208.mvvm.BaseViewActivity
import com.github.gzuliyujiang.wheelpicker.AddressPicker
import com.github.gzuliyujiang.wheelpicker.DatePicker
import com.github.gzuliyujiang.wheelpicker.OptionPicker
import com.github.gzuliyujiang.wheelpicker.annotation.AddressMode
import com.github.gzuliyujiang.wheelpicker.entity.DateEntity

@Route(path = "/app/order")
class OrderActivity : BaseViewActivity<ActivityOrderBinding>() {
    lateinit var orderAdapter: OrderAdapter
    override fun initData() {

    }

    override fun initView() {
        orderAdapter = OrderAdapter()
        binding.rvOrder.adapter = orderAdapter
        binding.rvOrder.layoutManager = LinearLayoutManager(this)

        val data = MyDBUtils.getDB().getOrderDao().queryStatus(0)//查询所有待下单的订单
        orderAdapter.data.clear()
        orderAdapter.data.addAll(data)
        orderAdapter.notifyDataSetChanged()

        var money = 0
        data.forEach {
            money+= it.count*it.goods_default_price
        }
        binding.tvMoney.setText("需要支付：${money} 元" )
        binding.btnPay.setOnClickListener {
            ARouter.getInstance().build("/app/pay").navigation()
        }
        //显示用户地址
        val address = SPUtils.getInstance().getString("address")
        binding.tvAddress.text = "用户地址: $address"

        //地址选择框
        binding.tvAddress.setOnClickListener {
//            val addressPicker = AddressPicker(this)
//            addressPicker.setTitle("选择配送地址")
//            addressPicker.setAddressMode(AddressMode.PROVINCE_CITY_COUNTY)//显示省 市 县
//            addressPicker.setOnAddressPickedListener { province, city, county ->
//                ToastUtils.showLong("${province.name}--${city.name}--${county.name}")
//                binding.tvAddress.setText( "用户地址: ${province.name}--${city.name}--${county.name}")
//                SPUtils.getInstance().put("address","${province.name}--${city.name}--${county.name}")
//            }
//            addressPicker.show()
//            val date = DatePicker(this)
//            date.setTitle("时间")
//            date.wheelLayout.setDefaultValue(DateEntity.today())//设置默认选中
//            date.wheelLayout.setRange(DateEntity.target(1900,1,1), DateEntity.today())
//            date.setOnDatePickedListener { year, month, day ->
//                ToastUtils.showLong("$year/$month/$day")
//            }
//            date.show()
            val list = mutableListOf("男","女","未知")
            val option = OptionPicker(this)
            option.setTitle("性别")
            option.setData(list)
            option.setOnOptionPickedListener { position, item ->
                ToastUtils.showLong("选择的是$item")
            }
            option.show()
        }
    }

    override fun getLayout(): Int {
        return R.layout.activity_order
    }

}